package com.qd.panda.stroke;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.util.page.PageUtil;
import com.qd.common.panda.domain.entity.order.OrderStatisticsDTO;
import com.qd.common.panda.domain.entity.order.UserStrokeOrderDisplayDTO;
import com.qd.common.panda.domain.entity.order.UserStrokeOrderQueryDTO;
import com.qd.common.panda.domain.entity.stroke.UserStrokeOrderDTO;
import com.qd.common.panda.domain.entity.stroke.UserStrokeOrderRefundDTO;
import com.qd.panda.service.stroke.order.UserOrderHolderService;
import com.qd.panda.service.stroke.order.UserStrokeOrderRefundService;
import com.qd.panda.service.stroke.order.UserStrokeOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * WEB_用户订单
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/sys/userOrder/")
@Slf4j
public class UserOrderController {
    private final UserOrderHolderService userOrderHolderService;
    private final UserStrokeOrderService userStrokeOrderService;
    private final UserStrokeOrderRefundService userStrokeOrderRefundService;

    /**
     * 用户订单统计
     *
     * @param dto 查询参数
     * @return 统计信息
     */
    @PreAuthorize("@ps.hasPermission('system:userOrder:totalOrderStatistics')")
    @GetMapping(value = "totalOrderStatistics")
    public OrderStatisticsDTO totalOrderStatistics(UserStrokeOrderQueryDTO dto) {
        return userOrderHolderService.totalOrderStatistics(dto);
    }

    /**
     * 用户订单列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @PreAuthorize("@ps.hasPermission('system:userOrder:getUserOrderPage')")
    @GetMapping(value = "getUserOrderPage")
    public IPage<UserStrokeOrderDisplayDTO> getUserOrderPage(UserStrokeOrderQueryDTO dto) {
        return userOrderHolderService.getOrderPage(PageUtil.buildPage(dto), dto);
    }

    /**
     * 用户订单详情
     *
     * @param orderId 订单ID
     * @return 订单详情
     */
    @PreAuthorize("@ps.hasPermission('system:userOrder:getUserOrderById')")
    @GetMapping(value = "getUserOrderById")
    public UserStrokeOrderDTO getUserOrderById(@Validated @NotBlank(message = "订单ID不能为空") String orderId) {
        return userStrokeOrderService.getByIdDto(orderId);
    }

    /**
     * 用户订导出
     *
     * @param dto 查询参数
     */
    @PreAuthorize("@ps.hasAnyPermissions('parking:order:export,system:userOrder:exportUserOrder')")
    @GetMapping(value = "exportUserOrder")
    public void exportUserOrder(UserStrokeOrderQueryDTO dto) {
        userOrderHolderService.export(dto);
    }

    /**
     * 退款列表
     *
     * @param orderId 订单ID
     * @return 订单详情
     */
    @PreAuthorize("@ps.hasPermission('system:userOrder:getUserRefundOrderList')")
    @GetMapping(value = "getUserRefundOrderList")
    public List<UserStrokeOrderRefundDTO> getUserRefundOrderList(@Validated @NotBlank(message = "订单ID不能为空") String orderId) {
        return userStrokeOrderRefundService.getListDTO(orderId);
    }
}